{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 106 - Quantile Regression with VowpalWabbit\n",
    "\n",
    "We will demonstrate how to use the VowpalWabbit quantile regressor with\n",
    "TrainRegressor and ComputeModelStatistics on the Triazines dataset.\n",
    "\n",
    "\n",
    "This sample demonstrates how to use the following APIs:\n",
    "- [`TrainRegressor`\n",
    "  ](http://mmlspark.azureedge.net/docs/pyspark/TrainRegressor.html)\n",
    "- [`VowpalWabbitRegressor`\n",
    "  ](http://mmlspark.azureedge.net/docs/pyspark/VowpalWabbitRegressor.html)\n",
    "- [`ComputeModelStatistics`\n",
    "  ](http://mmlspark.azureedge.net/docs/pyspark/ComputeModelStatistics.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "triazines = spark.read.format(\"libsvm\")\\\n",
    "    .load(\"wasbs://publicwasb@mmlspark.blob.core.windows.net/triazines.scale.svmlight\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print some basic info\n",
    "print(\"records read: \" + str(triazines.count()))\n",
    "print(\"Schema: \")\n",
    "triazines.printSchema()\n",
    "triazines.limit(10).toPandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Split the dataset into train and test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train, test = triazines.randomSplit([0.85, 0.15], seed=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Train the quantile regressor on the training data.\n",
    "\n",
    "Note: have a look at stderr for the task to see VW's output\n",
    "\n",
    "Full command line argument docs can be found [here](https://github.com/VowpalWabbit/vowpal_wabbit/wiki/Command-Line-Arguments).\n",
    "\n",
    "Learning rate, numPasses and power_t are exposed to support grid search."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mmlspark.vw import VowpalWabbitRegressor\n",
    "model = (VowpalWabbitRegressor(numPasses=20, args=\"--holdout_off --loss_function quantile -q :: -l 0.1\")\n",
    "            .fit(train))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Score the regressor on the test data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scoredData = model.transform(test)\n",
    "scoredData.limit(10).toPandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute metrics using ComputeModelStatistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mmlspark.train import ComputeModelStatistics\n",
    "metrics = ComputeModelStatistics(evaluationMetric='regression',\n",
    "                                 labelCol='label',\n",
    "                                 scoresCol='prediction') \\\n",
    "            .transform(scoredData)\n",
    "metrics.toPandas()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
